package com.qs.controller;

import com.qs.commen.Response;
import com.qs.pojo.SysResource;
import com.qss.service.ResourceService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @user HP
 * @email 1460961774@qq.com
 * @Date 2021-10-01 18:50
 * @packageName com.qs.controller
 */
@Controller
@RequestMapping("/resource")
public class ResourceController {
    @Reference
    private ResourceService service;

    @PostMapping("/data")
    @ResponseBody
    public Response queryAllResource(Integer roleId){
        List<SysResource> sysResources = service.queryAllResources(roleId);
        return new Response(200,sysResources);
    }

    @GetMapping("/toData")
    public String toData(){
        return "resource/data";
    }
    @PostMapping("/data2")
    @ResponseBody
    public Response queryAllResource2(SysResource resource){
        List<SysResource> sysResources2 = service.queryResources(resource);
        return new Response(200,sysResources2);
    }

    @GetMapping("toEdit")
    public String edit(SysResource resource, Model model){
        if (resource!=null&&resource.getResourceId()!=null){
           resource=  service.queryResourceById(resource.getResourceId());
            model.addAttribute("res",resource);
        }
        return "resource/edit";
    }

    @PostMapping("/save")
    @ResponseBody
    public Response saveResource(SysResource resource){
        boolean status=service.saveResource(resource);
        if (status){
            return Response.success("保存成功");
        }return Response.error();
    }

    @DeleteMapping("deletes")
    @ResponseBody
    public  Response delById(Integer[] ids){
        boolean status = service.delUserById(ids);
        if(status) return Response.success("删除成功！");
        return Response.error();
    }
    @PostMapping("checkName")
    @ResponseBody
    public  boolean check(SysResource resource){
     SysResource resource1= service.queryResourceByName(resource.getResourceName());
        if (resource1==null) return true;
        return false;
    }

}
